home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / Python-1.4 / Lib / site-python / dos.py < prev   
Text File  |  1998-06-24  |  4KB  |  159 lines

  1. """
  2. AMIGA LIBRARY INTERFACE FOR dos.library
  3. ©1996 Irmen de Jong, disclaimer applies!
  4.  
  5. $VER: dos.py 1.1 (17.11.96)
  6. """
  7.  
  8.  
  9. import string
  10. from doslib import *
  11.  
  12.  
  13. # Bits that signal you that a user has issued a break
  14. SIGBREAKF_CTRL_C = 1<<12
  15. SIGBREAKF_CTRL_D = 1<<13
  16. SIGBREAKF_CTRL_E = 1<<14
  17. SIGBREAKF_CTRL_F = 1<<15
  18.     
  19. # FILE PROTECTION BITS
  20. # USUAL
  21. FIBF_SCRIPT    = 1<<6
  22. FIBF_PURE    = 1<<5
  23. FIBF_ARCHIVE    = 1<<4
  24. FIBF_READ    = 1<<3
  25. FIBF_WRITE    = 1<<2
  26. FIBF_EXECUTE    = 1<<1
  27. FIBF_DELETE    = 1<<0
  28. # "OTHER"
  29. FIBF_OTR_READ    = 1<<15
  30. FIBF_OTR_WRITE    = 1<<14
  31. FIBF_OTR_EXECUTE= 1<<13
  32. FIBF_OTR_DELETE    = 1<<12
  33. # "GROUP"
  34. FIBF_GRP_READ    = 1<<11
  35. FIBF_GRP_WRITE    = 1<<10
  36. FIBF_GRP_EXECUTE= 1<<9
  37. FIBF_GRP_DELETE    = 1<<8
  38.  
  39. # FLAGS FOR DateToStr AND StrToDate
  40. DTF_SUBST    = 1<<0    # substitute Today, Tomorrow, etc.
  41. DTB_FUTURE    = 1<<1    # day of the week is in future
  42.  
  43. # FORMATS FOR DateToStr AND StrToDate
  44. FORMAT_DOS    = 0    # dd-mmm-yy
  45. FORMAT_INT    = 1    # yy-mm-dd
  46. FORMAT_USA    = 2    # mm-dd-yy
  47. FORMAT_CDN    = 3    # dd-mm-yy
  48.  
  49.  
  50.  
  51. #### ARGPARSER ##################################
  52.  
  53. class ArgParser:
  54.     """
  55.     Argument string parser class.
  56.     To be used for parsing dos.library/ReadArgs() arguments
  57.     (the Amiga style for command lines).
  58.     """
  59.     def __init__(self,template):            # create
  60.         self.new(template)
  61.     def new(self,template):                 # new template
  62.         self.template=template
  63.         self.reset()
  64.     def reset(self):                        # reset types & defaults
  65.         self.defaults,self.types = self.parsetempl(self.template)
  66.     def parse(self,args):
  67.         result=ReadArgs(self.template,args,self.types)
  68.         for k in result.keys():
  69.             if not result[k]:
  70.                 dflt = self.defaults[k]
  71.                 if type(dflt)!=type([]):
  72.                     result[k]=dflt
  73.                 else:
  74.                     result[k]=dflt[:]   # make slice copy of list
  75.         return result               
  76.  
  77.     # Below this point are private members.
  78.  
  79.     def parsetempl(self,templ):
  80.         # Parse template.
  81.         # This function builds the default argument dictionary and
  82.         # the argument type list.
  83.  
  84.         # first check special case: empty template
  85.         if not templ:
  86.             return ({},())
  87.  
  88.         for c in templ:
  89.             if not c in '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_.,=/ ':
  90.                 raise ValueError,'invalid char in template ('+c+')'
  91.  
  92.         # Split the template twice (first by ',' then by '/')
  93.         templ=map(lambda x: string.split(x,'/'),string.split(templ,','))
  94.         # Build the type list
  95.         types=[]; defdict={}; lists=0; keywdic={}
  96.         defaults = {'S':0, 'T':0, 'N':None, 'X':None, 'A':[], 'I': []}
  97.         for a in templ:
  98.             if a[0]=='':
  99.                 raise ValueError,'missing keyword in template'
  100.  
  101.             # Now convert argument spec --> type & modifiers.
  102.             # 6 different output types are possible:
  103.             # X     - string            \
  104.             # S,T   - bool (integer)     > can have /F as modifier
  105.             #    (note: currently \T is NOT SUPPORTED)
  106.             # N     - integer           /
  107.             # X/M   - array of strings ('A')
  108.             # N/M   - array of ints ('I')
  109.             # All 6 can have /A or /K as additional modifiers.
  110.             # However, /F /A and /K have no influence on the returned type. (/M does)
  111.  
  112.             type=None; modifiers='';
  113.             for s in map(string.upper,a[1:]):
  114.                 if not s:
  115.                     raise ValueError,'invalid template'
  116.                 elif s=='T':
  117.                     raise SystemError,'/T not (yet) supported, sorry'
  118.                 elif s in 'SNT':        # possible template option types
  119.                     if type:
  120.                         raise ValueError,'invalid switch combination at '+a[0]
  121.                     type=s
  122.                 elif s in 'KAMF':       # template option modifiers
  123.                     if s in modifiers or  \
  124.                        (s=='M' and 'F' in modifiers) or \
  125.                        (s=='F' and 'M' in modifiers):
  126.                         # do not allow /F/M or /M/F or /A/A etc 
  127.                         raise ValueError,'invalid switch combination at '+a[0]
  128.                     modifiers=modifiers+s
  129.                 else:
  130.                     raise ValueError,'unknown switch /'+s
  131.  
  132.             if not type:
  133.                 type = 'X'      # special type: string (default type)
  134.  
  135.             if 'M' in modifiers:
  136.                 lists=lists+1
  137.                 if lists>1:
  138.                     raise ValueError,'multiple /M switches'
  139.                 if type=='X': type='A'      # array of strings
  140.                 elif type=='N': type='I'    # array of ints
  141.                 else:
  142.                     raise ValueError,'wrong /M combination'
  143.  
  144.             # All done, add type to type list, and find default value.
  145.  
  146.             types.append(a[0],type)
  147.             try:
  148.                 keywdic[a[0]]=keywdic[a[0]]+1
  149.             except KeyError:
  150.                 keywdic[a[0]]=1
  151.             if not 'A' in modifiers:
  152.                 defdict[a[0]]=defaults[type]
  153.             
  154.         keywdic=keywdic.values()
  155.         if keywdic.count(1) != len(keywdic):
  156.             raise ValueError,'clashing keywords'
  157.  
  158.         return (defdict,tuple(types))
  159.